package runesim.node.net;
import runesim.node.model.*;
import runesim.node.packetbuilder.StaticPacketBuilder;
/**
 * These classes simplify network tasks, so you shouldn't have to mess about
 * with StaticPacketBuilders yourself. Those packets whose purpose I don't
 * understand are left as 'sendUnknownId'. NOTE: These packets DO NOT modify
 * player state. They simply send data.
 */
public class ActionSender {
	
	private Player player;
	
	public ActionSender(Player player) {
		this.player = player;
	}
	
	public void sendMapArea() {
		player.getIoSession().write(new StaticPacketBuilder().setId(73).addShortA(player.getLocation().getRegionX() + 6).addShort(player.getLocation().getRegionY() + 6).toPacket());
	}
	
	public void sendLoginResponse(int loginResponse, int playerGroup, int flag) {
		player.setPlayerGroup(playerGroup);
		player.getIoSession().write(new StaticPacketBuilder().setBare(true).addByte((byte) loginResponse).addByte((byte) playerGroup).addByte((byte) flag).toPacket());
	}
	
	public void sendLoginPacket() {
		player.getIoSession().write(new StaticPacketBuilder().setId(249)
		.addByteA((byte) 1) //TODO members T/F
		.addShortA(player.getIndex()) // player id
		.toPacket());
	}
	
	public void sendMessage(String s) {
		player.getIoSession().write(new StaticPacketBuilder().setId(253).setSize(Packet.Size.VariableByte).addString(s).toPacket());
	}
	
	public void sendPrivacySettings() {
		player.getIoSession().write(new StaticPacketBuilder().setId(206).addByte((byte) player.getPrivacySetting(0)).addByte((byte) player.getPrivacySetting(1)).addByte((byte) player.getPrivacySetting(2)).toPacket());
	}
	
	public void sendStat(int stat) {
		player.getIoSession().write(new StaticPacketBuilder().setId(134).addByte((byte) stat).addInt1((int)player.getXPs()[stat]).addByte((byte) player.getCurrentStat(stat)).toPacket());
	}
	
	public void sendSideBarInterface(int menuId, int form) {
		player.getIoSession().write(new StaticPacketBuilder().setId(71).addShort(form).addByteA((byte) menuId).toPacket());
	}
	
	public void sendLogout() {
		player.getIoSession().write(new StaticPacketBuilder().setId(109).toPacket());
	}
	
	public void sendResetCamera() {
		player.getIoSession().write(new StaticPacketBuilder().setId(107).toPacket());
	}
	
	public void sendPlayerCommand(int slot, boolean placeOnTop, String command) {
		player.getIoSession().write(new StaticPacketBuilder().setId(104).setSize(Packet.Size.VariableByte).addByte((byte) (-slot)).addByteA(placeOnTop ? (byte) 0 : (byte) 1).addString(command).toPacket());
	}
	
	public void sendLoginWindow(int recoveryChange, boolean memberWarning, int messages, int lastLoginIp, int lastLogin) {
		player.getIoSession().write(new StaticPacketBuilder().setId(176).addByte((byte) -recoveryChange).addShortA(messages).addByte(memberWarning ? (byte) 1 : (byte) 0).addInt2(lastLoginIp).addShort(lastLogin).toPacket());
	}
	
	public void sendClearScreen(){
		player.getIoSession().write(new StaticPacketBuilder().setId(219).toPacket());
	}
	
	public void sendInterface(int interfaceID){
		player.getIoSession().write(new StaticPacketBuilder().setId(97).addShort(interfaceID).toPacket());
	}
	
	public void sendEquipment(int type) {
		int itemId = 0, amount = 0;
		if(player.getEquipment().isEquipped(type)) {
			itemId = player.getEquipment().getID(type);
			amount = player.getEquipment().getAmount(type);
		}
		StaticPacketBuilder equip = new StaticPacketBuilder().setId(34).setSize(Packet.Size.VariableShort);
		equip.addShort(1688);
		equip.addByte((byte) type);
		
		if(itemId == 0) equip.addShort(itemId);
		else equip.addShort(itemId+1);
		
		if(amount > 254)
			equip.addByte((byte) 255).addInt(amount);
		else
			equip.addByte((byte) amount);
		player.getIoSession().write(equip.toPacket());
	}
	
	public void sendEquipmentHardReset(){
		sendEquipment(0);
		sendEquipment(1);
		sendEquipment(2);
		sendEquipment(3);
		sendEquipment(4);
		sendEquipment(5);
		sendEquipment(7);
		sendEquipment(9);
		sendEquipment(10);
		sendEquipment(12);
		sendEquipment(13);
	}
	
	public void sendInventoryItem(int id, int amount, int slot) {
		StaticPacketBuilder invItem =
			new StaticPacketBuilder().setId(34)
			.setSize(Packet.Size.VariableShort)
			.addShort(3214)
			.addByte((byte) slot)
			.addShort(id+1);
		if(amount > 254)
			invItem.addByte((byte) 255).addInt(amount);
		else
			invItem.addByte((byte) amount);
		player.getIoSession().write(invItem.toPacket());
	}

	public void sendResetInventory(){
		Item[] items = player.getInventory().getInventoryItems();
		StaticPacketBuilder invItemReset = new StaticPacketBuilder().setId(53).setSize(Packet.Size.VariableShort);
		invItemReset.addShort(3214); //frame to update
		invItemReset.addShort(28); //number of items
		
		for(int i = 0; i < items.length; i++) {
			if(items[i].getAmount() > 254) {
				invItemReset.addByte((byte)255);
				invItemReset.addInt2(items[i].getAmount());
			} else {
				invItemReset.addByte((byte) items[i].getAmount());
			}
			
			if(items[i].getID() == 0){
				invItemReset.addLEShortA(items[i].getID());
			} else invItemReset.addLEShortA(items[i].getID()+1); //item id
		}
		
		player.getIoSession().write(invItemReset.toPacket());
	}
	
	public void sendCameraShake(int intensity){
		StaticPacketBuilder cameraShake = new StaticPacketBuilder().setId(35);
		cameraShake.addByte((byte) 0);
		cameraShake.addByte((byte) intensity);
		cameraShake.addByte((byte) intensity);
		cameraShake.addByte((byte) intensity);
		player.getIoSession().write(cameraShake.toPacket());
	}
}